import { getEx, postEx,put } from './crud'


export function getRealAlarm(tid, aid, index) {
  return new Promise((re, rej) => {
    getEx("alarm/", "real?tid=" + tid + "&aid=" + aid + "&index=" + index).then(
      (res) => {
        if (res && res.data && res.data.result) {

          re(res.data.result)
        }
        else {
          re([])
        }

      }
    ).catch(err => {
      rej(err)
    });
  })

}

export function getAlarmHistory(tid, aid, index, begintime, endtime, ip, bi) {
  return new Promise((re, rej) => {
    if (!begintime) {
      begintime = ""
    }
    if (!endtime) {
      endtime = ""
    }
    if (!ip){
      ip = ""
    }
    if (!bi){
      bi = ""
    }
    getEx("alarm/", "histroy?tid=" + tid + "&aid=" + aid + "&index=" + index +
      "&begintime=" + begintime + "&endtime=" + endtime + "&ip=" + ip + "&bi=" + bi).then(
        (res) => {
          if (res && res.data && res.data.result) {
            console.log("-------------", res.data.result);
            re(res.data.result)
          }
          else {
            re([])
          }

        }
      ).catch(err => {
        rej(err)
      });
  })

}

export function syncAlarm(id) {
  return postEx('alarm/' + id)
}

export function syncAllAlarm() {
  return put("api/alarm/sync")
}


export default {
  getRealAlarm,
  getAlarmHistory,
  syncAlarm,
  syncAllAlarm
}